package com.jinlejia.bbs.adapter;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.bean.CommentDetailsBean;
import com.jinlejia.bbs.bean.base.InformationBean;
import com.jinlejia.bbs.databinding.BbsAdapterCommentsByAnswerItemBinding;

import java.util.ArrayList;

/**
 * @author 王进 on 2019/4/5.
 */

public class CommentsByAnswerAdapter extends BaseLoadAdapter<ArrayList<CommentDetailsBean>>{
    private final int TYPE_ITEM = 2;
    public CommentsByAnswerAdapter(Context context) {
        super(context);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater inflater, ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        switch (viewType){
            case TYPE_ITEM:
                viewHolder = new ItemViewHolder(inflater.inflate(R.layout.bbs_adapter_comments_by_answer_item, parent, false));
                break;
            default:
                throw new IllegalArgumentException();
        }
        return viewHolder;
    }

    @Override
    protected int getItemViewTypeZ(int position) {
        return TYPE_ITEM;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder){
            bindItemViewHolder((ItemViewHolder) holder,position);
        }
    }

    @Override
    public int getItemCount() {
        if (mData==null || mData.size()==0){
            return 1;
        }
        return mData.size();
    }

    //region Item
    private class ItemViewHolder extends RecyclerView.ViewHolder{

        ItemViewHolder(View itemView) {
            super(itemView);
            DataBindingUtil.bind(itemView);
        }
    }

    public interface DeleteCommentListener{
        void onDelete(String commentsCode);
    }

    private DeleteCommentListener deleteCommentListener;

    public void setDeleteCommentListener(DeleteCommentListener deleteCommentListener) {
        this.deleteCommentListener = deleteCommentListener;
    }

    private void bindItemViewHolder(ItemViewHolder holder, int position){
        BbsAdapterCommentsByAnswerItemBinding binding = DataBindingUtil.getBinding(holder.itemView);
        final CommentDetailsBean bean = mData.get(position);
        binding.setBean(bean);
        binding.commentedLayout.setVisibility(View.GONE);
        final InformationBean commentInfo = bean.getCommentInfo();
        boolean isOwn = commentInfo.isOwn();
        if (isOwn){
            binding.deleteTextView.setVisibility(View.VISIBLE);
        }else {
            binding.deleteTextView.setVisibility(View.GONE);
        }
        binding.deleteTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteCommentListener.onDelete(commentInfo.getCommentsCode());
            }
        });
    }
}
